/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx.wxs.controller;

import cn.webrx.wxs.mapper.FileCateMapper;
import cn.webrx.wxs.mapper.FileMapper;
import cn.webrx.wxs.vo.VoFileCate;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>Project: wxshop - AdminFileController
 * <p>Powered By webrx
 * <p>Created By IntelliJ IDEA On 2021-04-24 12:53:05
 * <p>Description :
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Controller
@RequestMapping("/admin")
public class AdminFileController {
    @Autowired
    private FileMapper fdao;
    @Autowired
    private FileCateMapper fcdao;

    @GetMapping("file/lists")
    @ResponseBody
    public Map<String, Object> lists(int type, int page_no, int page_size, @RequestParam(name = "cate", required = false, defaultValue = "0") String cate) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageHelper.startPage(page_no, page_size);
        PageInfo pi = PageInfo.of(fdao.page(cate, type));
        map.put("code", 1);
        map.put("msg", "");

        Map<String, Object> vv = new HashMap<String, Object>();
        vv.put("lists", pi.getList());
        vv.put("count", pi.getTotal());
        map.put("data", vv);
        return map;
    }


    @GetMapping("file/add")
    public String add(Model m) {
        return "/admin/file/add";
    }


    @GetMapping("file/image")
    public String image(Model m) {
        VoFileCate vfc = new VoFileCate();
        vfc.setId(0);
        vfc.setTitle("全部");
        vfc.setChildren(new ArrayList<VoFileCate>());
        List<VoFileCate> list = fcdao.queryCate();
        list.add(0, vfc);
        m.addAttribute("cate", list);
        return "/admin/file/image";
    }

    @PostMapping("file/del") @ResponseBody
    public Map<String,Object> del(int[] ids){
        Map<String,Object> map = new HashMap<String, Object>();
        map.put("code",1);
        map.put("msg","删除成功了，注意只删除数据库记录，没有彻底删除图片");
        //if(fdao.delById(ids)==0){
        //    map.put("code",0);
        //    map.put("msg","删除失败");
        //}

        return map;
    }

    /**
     * @param fs
     * @param req
     * @param cate
     * @return
     */

    @PostMapping("file/upload")
    @ResponseBody
    public Map<String, Object> image(@RequestParam("file") MultipartFile[] fs, HttpServletRequest req, int cate) {
        Map<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String pp = String.format("/uploads/images/background/%s/", sdf.format(new Date()));
        String path = req.getServletContext().getRealPath(pp);
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        int co = 0;
        for (MultipartFile f : fs) {
            String fn = f.getOriginalFilename();
            String nfn = UUID.randomUUID().toString().toLowerCase().replaceAll("-", "") + fn.substring(fn.lastIndexOf("."));
            String np = String.format("%s%s", path, nfn);
            try {
                f.transferTo(new File(np));
            } catch (Exception e) {
                e.printStackTrace();
            }
            cn.webrx.wxs.entity.File img = new cn.webrx.wxs.entity.File();
            img.setName(fn);
            img.setUri(String.format("%s%s", pp.substring(1), nfn));
            img.setCateId(cate);
            img.setType(1);
            fdao.save(img);
            co++;
        }
        map.put("code", 0);
        map.put("msg", "上传失败");
        if (co > 0) {
            map.put("code", 1);
            map.put("msg", "成功上传" + co + "个文件");
        }
        return map;
    }
}
